package com.hzsoft.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * <p>
 * 类描述：if标签
 * </p>
 * <p>
 * 创建人：liuyidong <br>
 * 创建时间：2012-5-4 上午11:42:53
 * </p>
 */
public class IfTag extends BodyTagSupport{ 
	  
	private static final long serialVersionUID = -6775152327717181056L;
	public IfTag() { 
        super(); 
        init(); 
    } 
  
    @Override
    public void release() { 
        super.release(); 
        init(); 
    } 
      
    @Override
    public int doStartTag() throws JspException { 
        if(test){ 
            this.succeeded(); 
        } 
        return EVAL_BODY_BUFFERED; 
    } 
  
    @Override
    public int doEndTag() throws JspException { 
        try { 
            if(subtagSucceeded) 
                pageContext.getOut().write(getBody()); 
        } catch (IOException e) { 
            throw new JspException("IOError while writing the body: " + e.getMessage(), e); 
        } 
          
        init(); 
        return super.doEndTag(); 
    } 
      
    private String body = null;     //  用于存放成功条件后的内容 
    public void setBody(){ 
        if(body == null){ 
            body = bodyContent.getString().trim(); 
        } 
    } 
      
    private String getBody(){ 
        if(body == null) 
            return bodyContent.getString().trim(); 
        else
            return body; 
    } 
      
    /** 
     * 判断if 或者 子 else if是否提交成功 
     */
    private boolean subtagSucceeded; 
      
    /** 
     * 子条件判断成功 
     */
    public void succeeded(){ 
        subtagSucceeded = true; 
    } 
    /** 
     * 是否已经执行完毕 
     * @return 
     */
    public boolean isSucceeded(){ 
        return subtagSucceeded; 
    } 
      
    private void init() { 
        test = false; 
        subtagSucceeded = false; 
        body = null; 
    } 
      
    private boolean test;   
      
    public void setTest(boolean test) { 
        this.test = test; 
    } 
}